<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\Abnormal;
use app\model\AbnormalType;
use app\model\Barn;
use app\model\Feed;
use app\model\Fence;
use app\model\Machine;
use app\model\Medicine;
use app\model\Pig;
use think\facade\Db;

class AbnormalLogic
{
    protected Abnormal $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Abnormal();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @param int $Id
     * @param array $param
     * @time 2024/03/12 14:50:44
     * @return true
     * @author: zj
     */
    public function addEdit(int $Id, array $param)
    {
        Db::startTrans();

        try {
            $exist = $this->model->field('id,out_id,case_id,abnormal_type as case_type,status,record_time,last_update_time')
                ->where('factory_id', $this->factory_id)
                ->where('out_id', $Id)
                ->where('delete_time', $this->model::IS_DELETE_NO)
                ->find();
            if ($exist) {
                $rule = [
                    'BarnId' => 'integer|>=:0',
                    'FenceId' => 'integer|>=:0',
                    'CaseId' => 'integer|>:0',
                    'CaseType' => 'integer|in:10,11,12,20,21,30',
                    'SecCaseType' => 'integer|>:0',
                    'Status' => 'integer|in:0,1',
                    'RecordTime' => 'require|integer|>:0',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if ($param['BarnId']) {
                    $updateData['barn_id'] = $param['BarnId'];
                }
                if ($param['FenceId']) {
                    $updateData['fence_id'] = $param['FenceId'];
                }
                if ($param['ErrorText']) {
                    $updateData['error_text'] = $param['ErrorText'];
                }
                if ($param['CaseId']) {
                    $updateData['case_id'] = $param['CaseId'];
                }
                if ($param['CaseType']) {
                    $updateData['abnormal_type'] = $param['CaseType'];
                }
                if ($param['SecCaseType']) {
                    $updateData['sec_abnormal_type_id'] = $param['SecCaseType'];
                }
                if (!empty($param['Status'])) {
                    $updateData['status'] = $param['Status'];
                }
                if (isset($param['Solution'])) {
                    $updateData['solution'] = $param['Solution'];
                }
                if ($param['RecordTime']) {
                    $updateData['last_update_time'] = $param['RecordTime'];
                }
                $updateData['update_time'] = request()->time();

                if ($param['CaseType'] || $param['CaseId']) {
                    if ($param['Status'] == $this->model::STATUS_PROCESSED) {
                        $this->changeCaseNormal($param['CaseType'] ?: $exist['case_type'], $param['CaseId'] ?: $exist['case_id'], $param['RecordTime']);
                    } else if ($param['Status'] == $this->model::STATUS_TODO) {
                        $this->changeCaseAbnormal($param['CaseType'] ?: $exist['case_type'], $param['CaseId'] ?: $exist['case_id'], $param['RecordTime']);
                    }
                }

                $this->model->where('out_id', $Id)
                    ->where('delete_time', $this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {
                $rule = [
                    'BarnId' => 'require|integer|>=:0',
                    'FenceId' => 'require|integer|>=:0',
                    'CaseId' => 'require|integer|>:0',
                    'CaseType' => 'require|integer|in:10,11,12,20,21,30',
                    'SecCaseType' => 'require|integer|>:0',
                    'Status' => 'require|integer|in:0,1',
                    'RecordTime' => 'require|integer|>:0',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'barn_id' => $param['BarnId'],
                    'fence_id' => $param['FenceId'],
                    'error_text' => $param['ErrorText'],
                    'case_id' => $param['CaseId'],
                    'abnormal_type' => $param['CaseType'],
                    'sec_abnormal_type_id' => $param['SecCaseType'],
                    'status' => $param['Status'],
                    'solution' => $param['Solution'],
                    'record_time' => $param['RecordTime'],
                    'last_update_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);

                $this->changeCaseAbnormal($param['CaseType'], $param['CaseId'], $param['RecordTime']);
            }

            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }

    /**
     * @Description 异常状态更新
     * @param string $caseType
     * @param int $Id
     * @time 2024/03/23 15:49:26
     * @return true
     * @author: zj
     */
    public function changeCaseAbnormal(string $caseType, int $Id, int $recordTime)
    {
        if ($caseType == AbnormalType::TYPE_BARN) {
            $model = new Barn();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_ABNORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_FENCE) {
            $model = new Fence();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_ABNORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_PIG) {
            $model = new Pig();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'health' => $model::STATUS_POTENTIAL_HEALTH,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_FEED) {
            $model = new Feed();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_ABNORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_MEDICINE) {
            $model = new Medicine();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_ABNORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_DEVICE) {
            $model = new Machine();
            $model->where('id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_ABNORMAL,
                    'update_time' => request()->time()
                ]);
        }

        return true;
    }

    /**
     * @Description 更新异常状态为正常
     * @param string $caseType
     * @param int $Id
     * @param int $recordTime
     * @time 2024/03/25 11:14:35
     * @return true
     * @author: zj
     */
    public function changeCaseNormal(string $caseType, int $Id, int $recordTime)
    {
        if ($caseType == AbnormalType::TYPE_BARN) {
            $model = new Barn();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_NORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_FENCE) {
            $model = new Fence();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_NORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_PIG) {
            $model = new Pig();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'health' => $model::STATUS_HEALTH,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_FEED) {
            $model = new Feed();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_NORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_MEDICINE) {
            $model = new Medicine();
            $model->where('out_id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_NORMAL,
                    'update_record_time' => $recordTime,
                    'update_time' => request()->time()
                ]);
        }
        if ($caseType == AbnormalType::TYPE_DEVICE) {
            $model = new Machine();
            $model->where('id', $Id)
                ->where('delete_time', $model::IS_DELETE_NO)
                ->update([
                    'status' => $model::STATUS_NORMAL,
                    'update_time' => request()->time()
                ]);
        }

        return true;
    }


}
